/**
 * 药品排名弹窗模块，点击图表首页后初始化弹窗数据
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","valueUtil","panelUtil","btnServ","datagridServ","formServ"],
		function($,sysmanMain,valueUtil,panelUtil,btnServ,datagridServ,formServ) {

		var rootid = 'panel-drug-percent-pop-drug';
		
		var window_id = 'drug-percent-pop-win';
		
		var module_code ='drugCountingInfo';
		var module_code_head = 'drugCounting';
		
		//类型定义
		var layerConfig = {
			init	: 'init',	//全院
			dept	: 'dept',		//科室
			doct	: 'doct',		//医生
			patient : 'patient'		//患者
		}
		
		var typeConfig = {
			all			: 'all',//门诊和住院
			inpatient	: 'in',	//住院标志
			outpatient	: 'out' //门诊标志
		}
		
		var defaultLayer = {
			layer	: layerConfig.dept,
			dept_id	: null, //当前科室id
			doct_id		: null,	//当前医生
			patient_id	: null //当前住院患者记录id
		}
		//当前查看信息
		var currentLayer = {
			dept_id	: null, //当前科室id
			
			doct_id		: null,	//当前医生
			
			patient_id	: null, //当前住院患者记录id
					
			type		: typeConfig.all,		//全部、门诊或住院
			layer		: layerConfig.dept		//当前层级
		}
		
		//格式化函数
		var formatValue=function(value,row,index){
			if (value > 0){
				return   (value*100).toFixed(2) +'%';							
			}else{
				return '0.00%';
			}
		}
		
		//基本字段定义
		var base_config = {
			//门诊字段
			base_out_columns	:[
				{field:'out_patient_num',title:'门诊人次',width:30,align:'center',halign:'center'},
				{field:'out_price_count',title:'门诊收人',width:30,align:'center',halign:'center'},
				{field:'out_drug_count',title:'药品收人',width:30,align:'center',halign:'center'},				
				{field:'out_drug_percent',title:'门诊药占比',width:30,align:'center',halign:'center',
					formatter: formatValue
				}
			],
			
			//住院字段
			base_in_columns	:[
				{field:'in_patient_num',title:'住院人次',width:30,align:'center',halign:'center'},
				{field:'in_price_count',title:'住院收人',width:30,align:'center',halign:'center'},
				{field:'in_drug_count',title:'药品收人',width:30,align:'center',halign:'center'},			
				{field:'in_drug_percent',title:'住院药占比',width:30,align:'center',halign:'center',
					formatter: formatValue
				},
				{field:'price_avg_day',title:'住院日均费用',halign:'center',width:30}
			],
			//合计字段
			base_all_columns	:[
				{field:'price_count',title:'费用总额',halign:'center',width:30},				
				{field:'drug_count',title:'药品费用',halign:'center',width:30},						
				{field:'drug_percent',title:'药占比',width:30,align:'center',halign:'center',
					formatter: formatValue
				}	
			]
		}
		
		var allColumns = base_config.base_out_columns.concat(base_config.base_in_columns,base_config.base_all_columns);
		//列定义
		var columns_config = {
			
			
			allColumns	: [allColumns],
			deptColumns	: [
				[{field:'dept_name',title:'科室名称',width:30,align:'center',halign:'center'}]
					.concat(allColumns)],
			doctColumns	: [
				[{field:'doct_name',title:'医生姓名',width:30,align:'center',halign:'center'}]
					.concat(allColumns)],
			patientOutColumns	: [[
				{field:'patient_name',title:'患者姓名',width:30,align:'center',halign:'center'},
				 {field:'out_price_count',title:'门诊费用',width:30,align:'center',halign:'center'},
				 {field:'out_drug_count',title:'药品费用',width:30,align:'center',halign:'center'},				
				 {field:'out_drug_percent',title:'门诊药占比',width:30,align:'center',halign:'center',
					formatter: formatValue
				 }
			]],
			patientInColumns	: [[
				{field:'patient_name',title:'患者姓名',width:30,align:'center',halign:'center'},
				{field:'zyhm',title:'住院号码',width:30,align:'center',halign:'center'},
				{field:'inhospitalat',title:'入院日期',width:30,align:'center',halign:'center'},
				{field:'outhospitalat',title:'出院日期',width:30,align:'center',halign:'center'},
				{field:'in_price_count',title:'住院费用',width:30,align:'center',halign:'center'},
				{field:'in_drug_count',title:'药品费用',width:30,align:'center',halign:'center'},			
				{field:'in_drug_percent',title:'住院药占比',width:30,align:'center',halign:'center',
					formatter: formatValue
				},
				{field:'price_avg_day',title:'住院日均费用',halign:'center',width:30}
			]]
		}
		
		
		var url = '/drugCounting/query/getDrugPercent.action';
		var queryParam ;
		
		var _buttonConfig;
		var public_method ={
			init:function(_options){
				//清除缓存
				currentLayer = defaultLayer;
				_buttonConfig = {
					'返回':private_method.backWay,
					'执行查询':private_method.doSearch
				}
				var _qryParam = {
					count_type : 'in'
				}
				
				$('#' + rootid + ' .condition-form').form('load',_qryParam);
				var _grid = $('#'+rootid + ' .dg-list')
				var _param = {
					url			: '/'+sysmanMain.getRootPath()+ url,
					rownumbers  : true,
					animate		: true,						
					striped		: true,
					border		: true,					
					idField		: 'id',						
					fitColumns	: true,
					
					onClickCell : private_method.editCell,
					columns		: columns_config.deptColumns
				}
				
				var _queryParams = $.extend({},_qryParam,private_method.getCondition())
				
				//获取查询参数
				_param.queryParams={condition:_queryParams}
				_grid.datagrid(_param);
				
				//绑定查询事件
				private_method.bindBtnEvent();
			}
			,
			/**
			 * 重建grid
			 * @param _options.layer	重建grid类型
			 */
			recreateGrid:function(_options){				
				
				var _grid = $('#'+rootid + ' .dg-list')
				var _param = {
					url			: '/'+sysmanMain.getRootPath()+ url,
					rownumbers  : true,
					animate		: true,						
					striped		: true,
					border		: true,					
					idField		: 'id',						
					fitColumns	: true,
					
					onClickCell : private_method.editCell,
					columns		: columns_config.allColumns
				}
				
				if(currentLayer.layer){
					switch(currentLayer.layer){
						case layerConfig.dept:
							_param.columns = columns_config.deptColumns;
							
							//清理数据
							currentLayer.dept_id = null;
							currentLayer.doct_id = null;
							currentLayer.patient_id = null;
							currentLayer.type = typeConfig.all;
							break;
						case layerConfig.doct:
							_param.columns = columns_config.doctColumns;
							
							//清理数据
							currentLayer.doct_id = null;
							currentLayer.patient_id = null;
							currentLayer.type = typeConfig.all;
							
							break;
						case layerConfig.patient:
						
							//不再响应表格点击事件
							_param["onClickCell"] = null;
							if(currentLayer.type){
								switch(currentLayer.type){
									case typeConfig.inpatient:
									_param.columns = columns_config.patientInColumns;
									break;
									case typeConfig.outpatient:
									_param.columns = columns_config.patientOutColumns;
									break;
								}
								
							}
							break;
						case layerConfig.init:
							currentLayer = defaultLayer;
						default:
							_param.columns = columns_config.deptColumns;
							currentLayer.type = typeConfig.all;
							break;
					}
				}
				
				var _queryParams = $.extend({},currentLayer,private_method.getCondition())
				
				//获取查询参数
				_param.queryParams={condition:_queryParams}
				_grid.datagrid(_param);
				
				//绑定查询事件
				private_method.bindBtnEvent();
			}
			,
			/**
			 * 药占比弹窗
			 */
			openPagePercent:function(_options){
				
				var config = {
					title	: '药占比',
					href	: '/'+sysmanMain.getRootPath()+'/' + module_code_head + '/query/popPageDrugPercent.action',
					window	: $('#' + window_id),
					maximized : true,
					resizable : false,
					flag	: false,					
					onClose : private_method.closePage
				}
				queryParam = _options;
				var _param = $.extend({},config,_options)
				datagridServ.windowEditCenter(_param);
			}
			
						
		}
		
		var private_method ={
			editCell:function(index,field,value){
				
				if(window.console)
					console.info(' editCell fired ...')
					
				
				switch(field){
					case 'out_patient_num':
					case 'out_price_count':
					case 'out_drug_count':
					case 'out_drug_percent':
						currentLayer.type = 'out';
						break;
					case 'in_patient_num':
					case 'in_price_count':
					case 'in_drug_count':
					case 'in_drug_percent':
						currentLayer.type = 'in';
						break;
					case 'price_avg_day':
					case 'price_count':
					case 'drug_count':
					case 'drug_percent':
						currentLayer.type = 'all';
						break;
						
				}
				
				var breadParam = {}
				var _g = $('#'+rootid + ' .dg-list')
				var rows = _g.datagrid('getRows')
				if(rows.length>=index){
					var row = rows[index];
					if(row){
						var checked = false;
						if(row.patient_id){
							//患者层
							currentLayer.patient_id = row.patient_id;
							currentLayer.layer = layerConfig.patient;
							
							breadParam.name = row.patient_name;
							checked = true;
						}
						
						if(!checked && row.doct_id){
							//当前在医生层
							currentLayer.doct_id = row.doct_id;
							
							//进入患者层
							currentLayer.layer = layerConfig.patient;
							breadParam.name = row.doct_name;
							checked = true;
							
							//清除数据
							currentLayer.patient_id = null;
						}
						if(!checked && row.dept_id){
							//当前在科室层
							currentLayer.dept_id = row.dept_id;
							
							//将要进入医生层
							currentLayer.layer = layerConfig.doct;
							breadParam.name = row.dept_name;
							//清除数据
							currentLayer.doct_id = null;
							currentLayer.patient_id = null;
						}
					}
					
					private_method.breadcrumb(breadParam),
					public_method.recreateGrid();
				}
				
			},
			/**
			 * 面包屑增加层级操作
			 * @param _options.name	显示名称
			 */
			breadcrumb:function(_options){
				
				var _el = $('#'+rootid + ' .breadcrumb')
				if(_el.length==1){
					var _h = '<li class="bread-item"><a href="#" class="bread-link" btn-type="'+ currentLayer.layer+'">' +_options.name+'</a></li>'
					_el.append(_h);
				}
				private_method.bindBreadEvent();
			}
			
			,
			/**
			 * 面包屑点击事件
			 */
			breadClick:function(e){
				if(e && e.currentTarget){
					var ce = $(e.currentTarget);
					var el = $(e.currentTarget).parent().nextAll();
					if(el.length>0){
						$(el).remove();
					}
					
					//设定当前层级
					var _type = ce.attr('btn-type')
					currentLayer.layer = _type;
					public_method.recreateGrid();
				}
			}
			,
			getCondition:function(){
				var _form = $('#'+ rootid +' .condition-form ')
				var ret = formServ.getDataObj({
					rootId:rootid,
					form : $(_form)
				});
				
				$.extend(ret,currentLayer);
				return ret;
			}
			,
			//执行查询
			doSearch:function(e){
				var ret = private_method.getCondition();

				var $dg = $('#' + rootid + " .dg-list")
				$($dg).datagrid('load', {condition:ret});
				if(e && e.currentTarget){
					$(e.currentTarget).linkbutton('enable');
				}
			}
			,
			
			closePage:function(){
				currentLayer = defaultLayer;
				$('#'+window_id).empty();
			},
			bindBtnEvent:function(){

				var window_btn = $('#'+rootid +' a.function-btn')
				$(window_btn).unbind('click')
				$(window_btn).bind('click',	private_method.function_btn_listenner)

			},
			
			/**
			 * 面包屑事件
			 */
			bindBreadEvent:function(){

				var window_btn = $('#'+rootid +' .bread-link')
				$(window_btn).unbind('click')
				$(window_btn).bind('click',	private_method.breadClick)

			},
			
			// 功能按钮监听函数
			function_btn_listenner : function(e) {
				if(window.console){
					console.log('got a return here~~~');
				}
				btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

			}
			
			
		}
			
		
		return public_method;
			
})